#include "adc.h"
#include "LPC_2148.h"
#include "tn.h"
#include "type.h"

/**
*******************************************************************************
  Function Name :init_adc0()

  Description :	Initialises the ADC0  

  Input :	None

  Output :	None

  Note : 
*******************************************************************************
*/	
void init_adc0(void)
{
  PINSEL1 = (PINSEL1 & ~(3 << 28)) | (1 << 28);
}

/**
*******************************************************************************
  Function Name :init_adc1()

  Description :	Initialises the ADC1  

  Input :	None

  Output :	None

  Note : 
*******************************************************************************
*/	
void init_adc1(void)
{

}


/**
*******************************************************************************
	Function Name : adc_read()

	Description   :

	Input         : adc number,channel

	Output        : 10 bit AD value

	Note          :
*******************************************************************************
*/

U16 adc_read(U8 adc_num, U8 ch)
{
  TN_INTSAVE_DATA
  U32 i=0;
  
  switch(adc_num)
  {
    case ADC0:
      tn_disable_interrupt();
      AD0CR = 0x00200D00 | (1<<ch);    // select channel
      AD0CR |= 0x01000000; // Start A/D Conversion
    
      do
      {
        i = AD0GDR; // Read A/D Data Register
      } while ((i & 0x80000000) == 0); // Wait for end of A/D Conversion
      tn_enable_interrupt();
      break;
    
    case ADC1:
      tn_disable_interrupt();
      AD1CR = 0x00200D00 | (1<<ch); // select channel
      AD1CR |= 0x01000000; // Start A/D Conversion
      do
      {
        i = AD1GDR; // Read A/D Data Register
      } while ((i & 0x80000000) == 0); // Wait for end of A/D Conversion
      tn_enable_interrupt();
      break;
  }
 
  return (i >> 6) & 0x03FF; // bit 6:15 is 10 bit AD value
}

unsigned long ISRTom_Counter = 0;

void extISRTom(void)
{
  ISRTom_Counter++;
  EXTINT = EXTINT_EINT3_MASK;      //clear interrupt  Writing a 1 to bit 3 clears the interrupt for EINT3
  VICVectAddr = 0;                 // reset interrupt
}

void init_my_interrupt(void)
{
// #define EXTMODE_EXTMODE3_MASK 0x8      (1=edge sensitive 0=level sensitive)
EXTMODE |= EXTMODE_EXTMODE3_MASK;                        /* INT3 edge trigger */
// #define EXTPOLAR_EXTPOLAR3_MASK 0x8    (1=rising edge 0=falling edge)
EXTPOLAR |= EXTPOLAR_EXTPOLAR3_MASK;    /* INT3 is falling edge by default  0xE01FC14C = 0x8 */
PINSEL1 = (PINSEL1 & ~(3 << 28)) | (1 << 29);     
// select EINT3  P0.30  (ADC J10 to disable)   0xE002C000 |= 0x80000000
VICIntSelect &= ~ VIC_BIT(VIC_EINT3);            // EINT3 selected as IRQ     0xFFFFF00C  EINT3 = bit 17
VICVectAddr10 = (unsigned int)extISRTom;                 // address of the ISR is extISRTom below
VICVectCntl10 = VIC_ENABLE | VIC_EINT3;   // (this is interrupt 17 and it is enabled)
VICIntEnable = VIC_BIT(VIC_EINT3);      //          0xFFFFF010     set bit 17
EXTINT &= ~EXTINT_EINT3_MASK;    //    Clear External Interrupt Flag Register
}








